import { defineConfig } from 'unocss'
import presetUno from '@unocss/preset-uno'
import presetIcons from '@unocss/preset-icons'
import presetAttributify from '@unocss/preset-attributify'
import transformerDirectives from '@unocss/transformer-directives'
import { FileSystemIconLoader } from '@iconify/utils/lib/loader/node-loaders'
import path from 'node:path'
import { globSync } from 'glob'

function getIcons() {
	const icons = {}
	const files = globSync('src/assets/icons/**/*.svg', { nodir: true })
	files.forEach(filePath => {
		const fileName = path.basename(filePath) // 获取文件名，包括后缀
		const fileNameWithoutExt = path.parse(fileName).name // 获取去除后缀的文件名
		const folderName = path.basename(path.dirname(filePath)) // 获取文件夹名
		if (!icons[folderName]) {
			icons[folderName] = []
		}
		icons[folderName].push(`i-${folderName}:${fileNameWithoutExt}`)
	})
	return icons
}

const icons = getIcons()
const collections = Object.fromEntries(
	Object.keys(icons).map(item => [item, FileSystemIconLoader(`src/assets/icons/${item}`)])
)

export default defineConfig({
	presets: [
		presetUno({ preflight: false }),
		presetAttributify(),
		presetIcons({
			prefix: 'i-',
			extraProperties: {
				display: 'inline-flex',
			},
			collections: {
				...collections,
				custom: {
					help: '<svg aria-hidden="true" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path fill="currentColor" fill-rule="evenodd" clip-rule="evenodd" d="M6.886 4.923c-.232.25-.386.63-.386 1.122a.75.75 0 1 1-1.5 0c0-.803.254-1.57.788-2.144C6.33 3.32 7.1 3 8 3c.9 0 1.67.32 2.212.901.534.574.788 1.341.788 2.144 0 1.234-.869 1.922-1.436 2.332-.094.068-.178.127-.255.181-.19.134-.33.233-.449.343a.64.64 0 0 0-.11.121V9.5a.75.75 0 0 1-1.5 0V9c0-.563.326-.956.593-1.202.198-.183.457-.364.672-.516l.17-.121c.559-.404.815-.693.815-1.116 0-.492-.154-.872-.386-1.122C8.888 4.68 8.533 4.5 8 4.5c-.533 0-.888.18-1.114.423Z"></path><path d="M9 12a1.001 1.001 0 0 1-2 0 1.001 1.001 0 0 1 2 0Z"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M8 14.5A6.5 6.5 0 1 0 1.5 8c0 1.022.179 1.608.603 2.399.316.59.407 1.313.178 2.003l-.66 1.976 1.977-.659a2.541 2.541 0 0 1 2.004.178c.79.424 1.376.603 2.398.603ZM8 16a8 8 0 1 0-8-8c0 1.29.25 2.117.78 3.107.136.252.168.549.078.82l-.807 2.42a1 1 0 0 0-.051.315V15a1 1 0 0 0 1 1h.338a1 1 0 0 0 .316-.051l2.419-.807c.271-.09.568-.057.82.078.99.53 1.817.78 3.107.78Z"></path></svg>',
					smtp: '<svg t="1747640620453" viewBox="0 0 1025 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3817" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M841.142857 73.142857a109.714286 109.714286 0 0 1 109.202286 99.181714L950.857143 182.857143v294.107428a36.571429 36.571429 0 0 1-72.557714 6.582858L877.714286 476.964571V194.340571L569.051429 459.776c-32.182857 19.529143-71.826286 21.211429-108.909715 2.121143l-10.020571-5.705143L146.285714 194.56v573.44a36.571429 36.571429 0 0 0 29.988572 35.986286L182.857143 804.571429h221.842286a36.571429 36.571429 0 0 1 6.582857 72.557714l-6.582857 0.585143H182.857143a109.714286 109.714286 0 0 1-109.202286-99.181715L73.142857 768v-585.142857a109.714286 109.714286 0 0 1 99.181714-109.202286L182.857143 73.142857h658.285714z m-19.675428 73.142857H202.24L492.982857 397.238857a40.740571 40.740571 0 0 0 29.257143 5.046857l3.949714-1.462857L821.467429 146.285714z" fill="currentColor" p-id="3818"></path><path d="M857.526857 626.395429l5.632 3.510857 146.651429 109.421714a36.571429 36.571429 0 0 1 5.558857 53.613714l-5.266286 4.827429-142.482286 108.544a36.571429 36.571429 0 0 1-49.225142-53.76l4.900571-4.388572 56.32-43.008-257.755429 0.073143a36.571429 36.571429 0 0 1-6.582857-72.557714l6.582857-0.585143H877.714286l-58.294857-43.593143a36.571429 36.571429 0 0 1 38.107428-62.098285z" fill="currentColor" p-id="3819"></path></svg>',
					mailbox:
						'<svg t="1747732902190" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="20987" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M838.83 280.42c-8.76-1.74-17.74-2.73-26.72-2.94l-11.05-2.49V154.13c0-37.89-33.19-68.72-74-68.72H288.34c-40.8 0-74 30.83-74 68.72v123.13l-1.23 0.2c-9.26 0.16-18.48 1.13-26.52 2.74C117.35 291.12 67.1 346.02 67.1 410.75V813.9c0 68.89 60.82 124.95 135.57 124.95h618.86c74.75 0 135.56-56.06 135.56-124.95V410.75c0.01-64.74-50.25-119.63-118.26-130.33z m-37.21 58.36l6.83 0.76c6.15 0 12.29 0.61 19.49 2.01 7.34 1.15 14.14 3.36 20.6 6.04l-47.49 31.93v-40.86l0.57 0.12zM288.34 147.51h438.72c7.38 0 11.9 4.29 11.9 6.62v267.14L548.81 549.12l-4.63 3.81c-17.29 17.28-44.36 18.39-60.31 2.44L276.45 414.64V154.13c0-2.34 4.51-6.62 11.89-6.62z m-90.9 193.83c5.58-1.1 11.26-1.58 16.91-1.66v32.83l-37.33-25.32c6.4-2.56 13.13-4.7 20.42-5.85zM895 813.9c0 34.66-32.95 62.85-73.46 62.85H202.67c-40.51 0-73.47-28.19-73.47-62.85V410.75c0-6.5 1.33-12.73 3.28-18.75l309.81 209.54c19.35 18.14 44.28 27.17 69.49 27.17 26.4-0.01 53.11-9.91 73.98-29.59l306.33-205.98c1.71 5.68 2.91 11.52 2.91 17.61V813.9z" fill="currentColor" p-id="20988"></path><path d="M517.91 465.9c45.28 0.75 83.39-13.21 114.34-41.89l-18.11-22.64c-27.17 19.62-58.49 29.43-93.96 29.43-65.66-0.76-98.87-36.6-99.62-107.54 4.53-70.93 38.11-109.05 100.75-114.34 59.61 1.52 90.56 33.59 92.83 96.22-1.52 38.49-12.45 59.25-32.83 62.26-7.55 0.76-10.95-2.64-10.19-10.19 0-3.77 0.37-8.3 1.13-13.58l12.45-105.28h-35.1l-1.13 11.32c-6.79-9.06-16.23-13.58-28.3-13.58-45.28 3.78-70.57 35.09-75.85 93.96-1.52 47.55 14.33 71.7 47.55 72.45 18.86 0 33.96-8.3 45.28-24.9 0.75 17.36 13.58 26.04 38.49 26.04 46.03-4.53 70.93-38.11 74.72-100.75-4.53-81.51-47.17-124.53-127.92-129.05-86.04 6.04-132.08 55.85-138.11 149.43 1.5 93.56 46.02 141.11 133.58 142.63z m-16.98-97.36c-15.85 0-23.02-12.07-21.51-36.23 5.28-37.73 19.62-58.11 43.02-61.13 11.32 0.76 17.73 10.19 19.24 28.3-3.78 41.52-17.37 64.53-40.75 69.06z" fill="currentColor" p-id="20989"></path></svg>',
				},
			},
		}),
	],
	transformers: [transformerDirectives({ enforce: 'pre' })],
	theme: {
		colors: {
			primary: 'var(--color-primary-1)',
			primaryHover: 'var(--color-primary-hover-1)',
			error: 'var(--color-error-1)',
			warning: 'var(--color-warning-1)',
			basic: 'var(--color-text-1)',
			default: 'var(--color-text-2)',
			desc: 'var(--color-text-3)',
		},
	},
	shortcuts: {
		'flex-center': 'flex items-center justify-center',
	},
})
